package com.supermap.qdsearch.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.supermap.qdsearch.R;
import com.supermap.qdsearch.utils.ConstUtils;
import com.supermap.qdsearch.utils.SPUtils;
import com.supermap.qdsearch.utils.ToastUtils;
import com.yanzhenjie.alertdialog.AlertDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.ibtn_settting)
    ImageButton ibtnSettting;
    @BindView(R.id.ll_login)
    LinearLayout llLogin;
    @BindView(R.id.ibtn_return)
    ImageButton ibtnReturn;
    @BindView(R.id.btn_revert)
    TextView btnRevert;
    @BindView(R.id.btn_save)
    TextView btnSave;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_ip)
    EditText etIp;
    @BindView(R.id.et_prot)
    EditText etProt;
    @BindView(R.id.swh_status)
    Switch swhStatus;
    @BindView(R.id.tv_away)
    TextView tvAway;
    private SPUtils spUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        spUtils = new SPUtils(ConstUtils.SP_KEY);

    }

    @OnClick({R.id.ibtn_settting, R.id.btn_login, R.id.ibtn_return, R.id.btn_revert, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_settting:
                llLogin.setVisibility(View.GONE);
                llSetting.setVisibility(View.VISIBLE);

                String ip = spUtils.getString("ip",getResources().getString(R.string.param_map_path));
                String port = spUtils.getString("port",getResources().getString(R.string.param_port));
                String status = spUtils.getString("status",getResources().getString(R.string.param_status));
                etIp.setText(ip);
                etProt.setText(port);
                swhStatus.setChecked("在线".equals(status)?true:false);
                break;
            case R.id.btn_login:
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                if("admin".equals(username)&&"admin".equals(password)){
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;

            case R.id.ibtn_return:
                llLogin.setVisibility(View.VISIBLE);
                llSetting.setVisibility(View.GONE);
                break;
            case R.id.btn_revert:
                llLogin.setVisibility(View.VISIBLE);
                llSetting.setVisibility(View.GONE);
                break;
            case R.id.btn_save:
                ip = etIp.getText().toString();
                port = etProt.getText().toString();
                status = tvAway.getText().toString();


                spUtils.putString("ip", ip);
                spUtils.putString("port", port);
                spUtils.putString("status", status);

                Toast.makeText(this, "保存成功，请登录", Toast.LENGTH_SHORT).show();

                llLogin.setVisibility(View.VISIBLE);
                llSetting.setVisibility(View.GONE);
                break;
        }
    }


    @OnCheckedChanged(R.id.swh_status)
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.swh_status:
                tvAway.setText(isChecked==true?"在线":"离线");
                break;

        }
    }


    long lasttime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (System.currentTimeMillis() - lasttime <= 2000) {

                AlertDialog.Builder builder = AlertDialog.newBuilder(LoginActivity.this);
                AlertDialog dialog = builder.setTitle("温馨提示").setMessage("您确定要退出应用吗？").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Process.killProcess(Process.myPid());
                        System.exit(0);
                    }
                }).setCancelable(true).show();
            } else {
                ToastUtils.showShortToast("再次点击，将退出应用！");
                lasttime = System.currentTimeMillis();
            }


        }
        return true;
    }
}

